package at.fhj.itm11.antesk.memory.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import at.fhj.itm11.antesk.memory.model.Logic;

/**
 * The main window for the game.
 * 
 * @author Ivan Antes-Klobucar
 * @version 1.0
 *
 */
public class GameWindow extends JFrame{

	private static final long serialVersionUID = -2752956918427674221L;
	private JMenuBar menuBar;
	
	private JMenu menu;
	private JMenuItem newGame;
	private JMenuItem exitGame;
	
	private JMenu help;
	private JMenuItem about;
	
	private FieldPanel fp;
	private StatusBar statusBar;

	/**
	 * Default constructor for this class.
	 */
	public GameWindow(){
		setSize(360,420);
		setTitle("Memory 1.1");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		setResizable(false);
//		setUndecorated(true);
//		setAlwaysOnTop(true);
		setLocationRelativeTo(null);
	}
	
	public FieldPanel getFp() {
		return fp;
	}

	/**
	 * Creates and adds the FieldPanel to the main window.
	 */
	public void addFieldPanel(int rows, int cols) {
		fp = new FieldPanel(rows,cols);
		getContentPane().add(fp, BorderLayout.CENTER);
	}

	/**
	 * Creates and adds the menu bar to the main window.
	 */
	public void addMenuBar() {
		menuBar = new JMenuBar();
		menu = new JMenu("Game");
		newGame = new JMenuItem("New Game");
		newGame.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				GameWindow.this.remove(fp);
				GameWindow.this.remove(statusBar);
				
				fp = new FieldPanel(4,4);
				statusBar = new StatusBar();
				
				for(int i = 0; i < fp.getFields().length; i++){
					fp.getFields()[i].setEnabled(true);
				}
				
				GameWindow.this.add(fp, BorderLayout.CENTER);
				GameWindow.this.add(statusBar, BorderLayout.PAGE_END);
				GameWindow.this.validate();
				
				Logic lg = Logic.getInstance();
				lg.getP().setScore(0);
				lg.setElapsedTime(0);
				lg.getT().start();
				lg.setUncovered(0);
				lg.setClickCounter(0);
			}
		});
		exitGame = new JMenuItem("Exit");
		exitGame.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		help = new JMenu("Help");
		about = new JMenuItem("About");
		about.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new AboutWindow();
			}
		});
		help.add(about);
		
		menu.add(newGame);
		menu.add(exitGame);
		menuBar.add(menu);
		menuBar.add(help);
		setJMenuBar(menuBar);
	}
	
	public void addStatusBar(){
		statusBar = new StatusBar();
		add(statusBar, BorderLayout.PAGE_END);
	}
	
	public StatusBar getStatusBar() {
		return statusBar;
	}

	/**
	 * Displays the main window.
	 */
	public void open(){
		setVisible(true);
	}
}
